package com.sailmi.oss.support;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author Tomonori
 * @mail gutrse3321@live.com
 * @data 2021-04-28 16:48
 */
public class Base64DecodedMultipartFile implements MultipartFile {

	private final byte[] imgContent;

	public Base64DecodedMultipartFile(byte[] imgContent) {
		this.imgContent = imgContent;
	}

	@Override
	public String getName() {
		return System.currentTimeMillis() + Math.random() + ".jpg";
	}

	@Override
	public String getOriginalFilename() {
		return System.currentTimeMillis() + (int)Math.random() * 10000 + ".jpg";
	}

	@Override
	public String getContentType() {
		return "jpg";
	}

	@Override
	public boolean isEmpty() {
		return imgContent == null || imgContent.length == 0;
	}

	@Override
	public long getSize() {
		return imgContent.length;
	}

	@Override
	public byte[] getBytes() {
		return imgContent;
	}

	@Override
	public InputStream getInputStream() {
		return new ByteArrayInputStream(imgContent);
	}

	@Override
	public void transferTo(File dest) throws IOException, IllegalStateException {
		new FileOutputStream(dest).write(imgContent);
	}
}
